package com.pureut.production.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.production.domain.CalendarTeam;
import com.pureut.production.domain.SchedulingCalendar;
import com.pureut.production.domain.SchedulingCalendarTeam;
import com.pureut.production.domain.dto.SchedulingCalendarMqDto;
import com.pureut.production.mapper.SchedulingCalendarMapper;
import com.pureut.production.mapper.SchedulingCalendarTeamMapper;
import com.pureut.production.service.SchedulingCalendarService;
import com.pureut.production.service.SchedulingCalendarTeamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/***
 *
 * Author:C
 * Date:2023/04/11 9:38
 * @DESC
 */
@Service
public class SchedulingCalendarServiceImpl extends ServiceImpl<SchedulingCalendarMapper, SchedulingCalendar> implements SchedulingCalendarService {

    @Resource
    SchedulingCalendarMapper schedulingCalendarMapper;

    @Resource
    SchedulingCalendarTeamService schedulingCalendarTeamService;

    @Resource
    SchedulingCalendarTeamMapper schedulingCalendarTeamMapper;

    /**
     * 新增
     *
     * @param schedulingCalendarMqDto
     */
    @Override
    public void insertSchedulingCalendar(SchedulingCalendarMqDto schedulingCalendarMqDto) {
        if (schedulingCalendarMqDto.getCalendarCode() != null) {

            SchedulingCalendar schedulingCalendar = new SchedulingCalendar();
            BeanUtils.copyProperties(schedulingCalendarMqDto, schedulingCalendar);
            boolean save = save(schedulingCalendar);
            if (save) {
                List<CalendarTeam> calendarList = schedulingCalendarMqDto.getCalendarList();
                List<SchedulingCalendarTeam> resultList = new ArrayList<>();
                for (CalendarTeam entity : calendarList) {
                    SchedulingCalendarTeam schedulingCalendarTeam = new SchedulingCalendarTeam();
                    BeanUtils.copyProperties(entity, schedulingCalendarTeam);
                    schedulingCalendarTeam.setCalendarId(schedulingCalendarMqDto.getCalendarId());
                    resultList.add(schedulingCalendarTeam);
                }
                schedulingCalendarTeamService.saveBatch(resultList);
            }
        }
    }

    /**
     * 修改
     *
     * @param schedulingCalendarMqDto
     */
    @Override
    public void updateSchedulingCalendar(SchedulingCalendarMqDto schedulingCalendarMqDto) {
        SchedulingCalendar schedulingCalendar = schedulingCalendarMapper.selectOne(new QueryWrapper<SchedulingCalendar>().lambda().eq(SchedulingCalendar::getCalendarCode, schedulingCalendarMqDto.getCalendarCode()));
        if (schedulingCalendar != null) {
            BeanUtils.copyProperties(schedulingCalendarMqDto, schedulingCalendar);
            schedulingCalendarMapper.updateSchedulingCalendar(schedulingCalendar);
            //删除之前的明细数据
            List<SchedulingCalendarTeam> schedulingCalendarTeams = schedulingCalendarTeamMapper.selectList(new QueryWrapper<SchedulingCalendarTeam>().lambda().eq(SchedulingCalendarTeam::getCalendarId, schedulingCalendar.getCalendarId()));
            List<Long> itemIds = new ArrayList<>();
            for (SchedulingCalendarTeam entity : schedulingCalendarTeams) {
                itemIds.add(entity.getTeamId());
            }
            int i1 = schedulingCalendarTeamMapper.removeTeamBatchByIds(itemIds);
            if (i1 > 0) {
                List<CalendarTeam> calendarList = schedulingCalendarMqDto.getCalendarList();
                List<SchedulingCalendarTeam> resultList = new ArrayList<>();
                for (CalendarTeam entity : calendarList) {
                    SchedulingCalendarTeam schedulingCalendarTeam = new SchedulingCalendarTeam();
                    BeanUtils.copyProperties(entity, schedulingCalendarTeam);
                    schedulingCalendarTeam.setCalendarId(schedulingCalendarMqDto.getCalendarId());
                    resultList.add(schedulingCalendarTeam);
                }
                schedulingCalendarTeamService.saveBatch(resultList);
            }
        }
    }
}
